﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using Rung.Net.Base;
using Rung.Net.Events;
using Rung.Net.Cache;

namespace Rung.Net.Cache
{
    public class CacheClient : NetBaseClient<byte[]>
    {
        public bool IsBusy
        {
            get
            {
                if (this.stream is CacheClientStream)
                    return (this.stream as CacheClientStream).IsBusy;
                else
                    throw (new InvalidCastException("缓存流类型格式转换错误。"));
            }
        }

        /// <summary>
        /// 传输进度更改事件。
        /// </summary>
        public event CacheProcessChangedEventHandler OnCacheProcessChanged;
        /// <summary>
        /// 文件传输完成事件。
        /// </summary>
        public event FileTransferComplitedEventHandler OnFileTransferComplited;

        protected override NetBaseStream<byte[]> CreateStream(NetworkStream ns, EndPoint ep)
        {
            CacheClientStream s = new CacheClientStream(ns, ep);

            if (this.OnCacheProcessChanged != null)
                s.OnCacheProcessChanged += this.OnCacheProcessChanged;

            if (this.OnFileTransferComplited != null)
                s.OnFileTransferComplited += this.OnFileTransferComplited;

            return s;
        }

        public void Send(string fullFileName)
        {
            CacheStream s = this.stream as CacheStream;
            if (s == null) throw (new NullReferenceException("数据流为空引用。"));

            s.Send(fullFileName);
        }
    }
}
